//
//  SelectCoinViewController.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/5/5.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit
typealias selectCoinBlock = (Int)->()
class SelectCoinViewController: BaseViewController, SelectCoinDelegate {
    var selectBlock:selectCoinBlock?
    var selectIndex = 0
    var type:CoinAccountType?
    var isTransfer:Bool = false
    //币币币种
    lazy var dateArr:Array<AssetModel> = {
        let array:Array<AssetModel> = []
        return array
    }()
    
    lazy var legalArr:Array<AssetModel> = {
        let array:Array<AssetModel> = []
        return array
    }()
    
    lazy var leverArr:Array<AssetModel> = {
        let array:Array<AssetModel> = []
        return array
    }()
    
    var sendType:CoinAccountType?
    var receiveType:CoinAccountType?
    
    lazy var selectCoinView:SelectCoinView = {
        let view = SelectCoinView(dataSource: self)
        view.delegate = self
        return view
    }()
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.title = kText("选择币种")
        view.addSubview(selectCoinView)
        selectCoinView.selectIndex = selectIndex
        selectCoinView.snp.makeConstraints { (make) in
            make.top.left.bottom.right.equalToSuperview()
        }
        var currentArr:[AssetModel] = []
        if isTransfer {
        if sendType == .legalType {
            currentArr = legalArr
        }else if sendType == .coinType {
            currentArr = dateArr
        }else {
            currentArr = dateArr
        }
        }else {
            currentArr = dateArr
        }
        //交集
        var intersection:[AssetModel] = []
        if sendType == .legalType || receiveType == .legalType  {
            (currentArr as NSArray).enumerateObjects({ (data, index, stop) in
                (legalArr as NSArray).enumerateObjects({ (source, position, end) in
                    if (source as? AssetModel)?.coinCode ==  (data as? AssetModel)?.coinCode {
                        intersection.append((source as! AssetModel))
                        end.initialize(to: true)
                    }
                })
            })
            currentArr = intersection
        }
        
        selectCoinView.setData(array: currentArr)
    }
    override func viewWillDisappear(_ animated: Bool) {
         super.viewWillDisappear(animated)
        
    }
    
    func selectIndex(index: Int) {
        if selectBlock != nil {
            selectBlock!(index)
        }
        self.navigationController?.popViewController(animated: true)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
